/**
 * @author Tobias Boehm
 *
 * 13.04.2011
 */
package org.anddev.andengine.extension.tbsengine.tool;

import org.anddev.andengine.extension.tbsengine.menu.activity.AbsFullscreenActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;

/**
 * This is an activity for patching internal data structures (such as SQL tables
 * and directory hierarchies) after a *.apk update.
 * 
 * @author Tobias Boehm
 *
 */
public abstract class AbsStartupCheckActivity extends AbsFullscreenActivity {

	protected static final int DEFAULT_LAST_STARTUP_VERSION = -1; 
	protected static final int DEFAULT_STARTUP_COUNT = 0;
	
	private static final String TBSE_LAST_STARTUP_APPLICATION_VERSION = "TBSE_LAST_STARTUP_APPLICATION_VERSION";
	private static final String TBSE_OVERALL_STARTUP_COUNT = "TBSE_OVERALL_STARTUP_COUNT";
	private static final String TBSE_STARTUPS_WITH_VERSION_ = "TBSE_STARTUPS_WITH_VERSION_";
	
	
	protected abstract Intent onStartupVersionCheck(final int pLastStartupApplicationVersion,final int pThisStartupVersion, final int pStartupsWithCurrentVersion, final int pOveraallStartups,final SharedPreferences pSharedPreferences);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		this.checkStartUp();
	}


	private void checkStartUp() {
		
		// Reading Values
		final SharedPreferences sharedPreferences = getSharedPreferences(this.getSharedPreferencesFilname(), MODE_PRIVATE);
		
		final int lastStartupApplicationVersion = sharedPreferences.getInt(TBSE_LAST_STARTUP_APPLICATION_VERSION,DEFAULT_LAST_STARTUP_VERSION);
		
		int currentVersion = DEFAULT_LAST_STARTUP_VERSION;
		
		try {
			currentVersion = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA).versionCode;
		} catch (NameNotFoundException e) {}
		
		int overallStarups = sharedPreferences.getInt(TBSE_OVERALL_STARTUP_COUNT, DEFAULT_STARTUP_COUNT);
		overallStarups++;
		
		int startupsWithCurrentVersion = sharedPreferences.getInt(TBSE_STARTUPS_WITH_VERSION_ + currentVersion, DEFAULT_STARTUP_COUNT);
		startupsWithCurrentVersion++;
		
		// If onStartupVersionCheck returns an Intent increment save the current Values
		final Intent nextActivityIntent = onStartupVersionCheck(lastStartupApplicationVersion, currentVersion, startupsWithCurrentVersion, overallStarups, sharedPreferences);
		
		if(nextActivityIntent != null){
			
			// Save data
			final Editor editor = sharedPreferences.edit();
			
			editor.putInt(TBSE_LAST_STARTUP_APPLICATION_VERSION, currentVersion);
			editor.putInt(TBSE_OVERALL_STARTUP_COUNT, overallStarups);
			editor.putInt(TBSE_STARTUPS_WITH_VERSION_+currentVersion, startupsWithCurrentVersion);
			
			editor.commit();
			
			// Execute Intent
			this.startActivity(nextActivityIntent);
		
		}else{
			
			// Kill Application
			finish();
		}
	}
	
	protected abstract String getSharedPreferencesFilname();
}
